package dao;

import com.mongodb.DBCollection;
import com.mongodb.WriteResult;
import com.mongodb.client.AggregateIterable;
import entity.Order;
import entity.Product;
import entity.User;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.FindOptions;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.UpdateOperations;
import org.mongodb.morphia.query.UpdateResults;
import util.MorphiaUtil;

import java.util.*;

import static util.MorphiaUtil.getDB;

public class OrderDAO {
    //添加
//    public void insert(Product product) {
//        Datastore ds = getDB();
//        ds.save(product);
//    }
//    //删除
//    public int deleteOne(ObjectId id){
//        Datastore db=getDB();
//        Query<Product> query= db.createQuery(Product.class);
//        query.filter("_id",id);
//        WriteResult delete = db.delete(query);
//        return delete.getN();
//    }
    //根据id查询
    public Product findById(ObjectId id){
        Datastore db=getDB();
        //创建查询条件对象
        Query<Product> query= db.createQuery(Product.class);
        //设置查询条件
        query.filter("_id",id);
        Product p=query.get();
        return p;
    }
    //查询全部
    public List<Order> findAll() {
        Datastore ds = getDB();
        Query<Order> query = ds.createQuery(Order.class);
        List<Order> orders = query.asList();
        return orders;
    }
    //分页查询
    public List<Order> findPage(int skip,int limit,Object userId) {
        Datastore ds = getDB();
        Query<Order> query = ds.createQuery(Order.class);
        if(userId != null)
            query.filter("userId",userId);
        FindOptions findOptions = new FindOptions();
        findOptions.skip(skip).limit(limit);
        List<Order> orders = query.asList(findOptions);
        return orders;
    }

    public long count(Object userId){
        //    通过Morphia框架获得database
        Datastore db = MorphiaUtil.getDB();
        //        创建查询条件对象query
        Query<Order> query = db.createQuery(Order.class);
        if(userId != null)
            query.filter("userId",userId);
        long count = query.count();
        return count;
    }

    public List<Map.Entry<String, Integer>> getTop4Area(Object userId) {
        Datastore ds = getDB();
        Query<Order> query = ds.createQuery(Order.class);
        if(userId != null)
            query.filter("userId",userId);
        List<Order> orders = query.asList();
        Map<String,Integer> map = new HashMap<>();
        for (Order order : orders) {
            String key = order.getRecvProvince();
            if(map.containsKey(key))
                map.put(key,map.get(key)+1);
            else
                map.put(key,1);
        }

        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
        Collections.sort(list,new Comparator<Map.Entry<String,Integer>>() {
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue()-o1.getValue();
            }
        });

        return list;
    }

//    public void updateStatus(String id){
//        Datastore ds = getDB();
//        OrderDAO productDAO=new OrderDAO();
//        Product product=productDAO.findById(new ObjectId(id));
//        System.out.println(product);
//
//
//        if(product.getStatus()==0){
//            product.setStatus(1);
//        }
//        else if(product.getStatus()==1){
//            product.setStatus(0);
//        }
//
//        ds.save(product);
//    }
}
